package cn.hyy.FVOnline.service.impl;

import cn.hyy.FVOnline.entity.Goods;
import cn.hyy.FVOnline.entity.PageEntitys;
import cn.hyy.FVOnline.entity.User;
import cn.hyy.FVOnline.service.EvaluateService;
import cn.hyy.FVOnline.service.UserService;
import cn.hyy.FVOnline.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hyy.FVOnline.mapper.EvaluateMapper;
import cn.hyy.FVOnline.entity.Evaluate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("evaluateService")
public class EvaluateServiceImpl implements EvaluateService{

    @Autowired
    private EvaluateMapper evaluateMapper;

    @Override
    public void savaEvaluate(Evaluate evaluate) {

        Long createTime = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setTime(createTime);
        evaluate.setTime(dateFormat.format(date));

        evaluateMapper.saveEvaluate(evaluate);

    }

    @Autowired
    private UserService userService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public PageEntitys getEvaluate(String what, Integer page, Integer pageSize) {
        what = "%" + what + "%";
        Integer start = (page - 1) * pageSize;
        List<Evaluate> evaluate = evaluateMapper.getEvaluate(what, start, pageSize);
        for (Evaluate e:evaluate) {
            User u = new User();
            u.setId(e.getUid());
            User user = userService.findById(u);
            e.setUser(user);
            Goods goods = goodsService.findById(e.getGid());
            e.setGoods(goods);
        }
        PageEntitys pageEntitys = new PageEntitys();
        pageEntitys.setEvaluates(evaluate);
        pageEntitys.setTotal(evaluateMapper.getEvaluateTotal(what));
        return pageEntitys;
    }

    @Override
    public void delEvaluate(Integer id) {
        evaluateMapper.delEvaluate(id);
    }
}
